<?php

namespace app\admin\service;

use app\admin\model\BoxGame;
use app\admin\model\BoxMemberltvLog;
use app\admin\model\BoxMemberpayLtvLog;
use think\Db;

class BoxStatisticsMemberpayltvService extends BaseService
{

    /**
     * @Notes:付费用户LTV
     * @Interface getList
     * @return array
     */
    public function getList()
    {
        $param = request()->param();

        $map = [];

        if (!empty($param['time_range'])) {
            $param['time_range'][0] = implode('', explode('-', $param['time_range'][0]));
            $param['time_range'][1] = implode('', explode('-', $param['time_range'][1]));
            $map[]                  = ['day_time', '>=', $param['time_range'][0]];
            $map[]                  = ['day_time', '<=', $param['time_range'][1]];
        }
        if (isset($param['box_game_id']) && !empty($param['box_game_id'])) {
            $map[] = ['box_game_id', '=', $param['box_game_id']];
        }

        $list = (new BoxMemberpayLtvLog())
            ->field('day_time,box_game_id,sum(pay_account_num) as pay_account_num,sum(d1) as d1,sum(d2) as d2,sum(d3) as d3,sum(d4) as d4,sum(d5) as d5,sum(d6) as d6,sum(d7) as d7,sum(d14) as d14,sum(d30) as d30,sum(d45) as d45,sum(d60) as d60,sum(d90) as d90,sum(d120) as d120,sum(d180) as d180,sum(d360) as d360')
            ->where($map)
            ->order('day_time', 'desc');

        if (empty($param['box_game_id'])) {
            $list->group('day_time');
        } else {
            $list->group('day_time,box_game_id');
        }

        $list = $list->select()->toArray();
        //获取数据总数
        $count = count($list);

        $gameData = BoxGame::field(['id', 'game_name'])->select()->toArray();
        $gameData = array_column($gameData, 'game_name', 'id');

        $result = array();
        if (!empty($list)) {

            $pay_account_num_all = 0;
            $total_ltv           = 0;
            $d1_all              = 0;
            $d2_all              = 0;
            $d3_all              = 0;
            $d4_all              = 0;
            $d5_all              = 0;
            $d6_all              = 0;
            $d7_all              = 0;
            $d14_all             = 0;
            $d30_all             = 0;
            $d45_all             = 0;
            $d60_all             = 0;
            $d90_all             = 0;
            $d120_all            = 0;
            $d180_all            = 0;
            $d360_all            = 0;

            foreach ($list as $key => $item) {
                $day     = diffTimeRetrunDay(strtotime($item['day_time']), strtotime(date("Y-m-d")));
                $day     = $day + 1;
                $dayList = ['1', '2', '3', '4', '5', '6', '7', '14', '30', '45', '60', '90', '120', '180', '360'];
                $dayNum     = findValue($day, $dayList);
                $dayNum  = "d" . $dayNum;

                $result[$key]['day_time']        = date("Y-m-d", strtotime($item['day_time']));
                $result[$key]['pay_account_num'] = $item['pay_account_num'];
                $result[$key]['game_name']       = empty($param['box_game_id']) ? '全部游戏' : $gameData[$item['box_game_id']] ?? '';
                $result[$key]['total_ltv']       = empty($item['pay_account_num']) ? "0" : round($item[$dayNum] / $item['pay_account_num'], 2);

                $num        = findMinValue($day, $dayList);
                foreach ($dayList as $dayItem) {
                    $dayItemNum = "d" . $dayItem;
                    if ($dayItem <= $num) {
                        $result[$key][$dayItemNum] = empty($item['pay_account_num']) ? "0" : round($item[$dayItemNum] / $item['pay_account_num'], 2);
                    } else {
                        $result[$key][$dayItemNum] = '0';
                    }
                }

                $pay_account_num_all += $result[$key]['pay_account_num'];
                $total_ltv           += $result[$key]['total_ltv'];
                $d1_all              += $result[$key]['d1'];
                $d2_all              += $result[$key]['d2'];
                $d3_all              += $result[$key]['d3'];
                $d4_all              += $result[$key]['d4'];
                $d5_all              += $result[$key]['d5'];
                $d6_all              += $result[$key]['d6'];
                $d7_all              += $result[$key]['d7'];
                $d14_all             += $result[$key]['d14'];
                $d30_all             += $result[$key]['d30'];
                $d45_all             += $result[$key]['d45'];
                $d60_all             += $result[$key]['d60'];
                $d90_all             += $result[$key]['d90'];
                $d120_all            += $result[$key]['d120'];
                $d180_all            += $result[$key]['d180'];
                $d360_all            += $result[$key]['d360'];
            }
            $result[$count]['day_time']        = "汇总";
            $result[$count]['pay_account_num'] = $pay_account_num_all;
            $result[$count]['game_name']       = "-";
            $result[$count]['total_ltv']       = empty($total_ltv) ? '0' : round($total_ltv / $count, 2) ?? '0';
            $result[$count]['d1']              = empty($d1_all) ? '0' : round($d1_all / $count, 2) ?? '0';
            $result[$count]['d2']              = empty($d2_all) ? '0' : round($d2_all / $count, 2) ?? '0';
            $result[$count]['d3']              = empty($d3_all) ? '0' : round($d3_all / $count, 2) ?? '0';
            $result[$count]['d4']              = empty($d4_all) ? '0' : round($d4_all / $count, 2) ?? '0';
            $result[$count]['d5']              = empty($d5_all) ? '0' : round($d5_all / $count, 2) ?? '0';
            $result[$count]['d6']              = empty($d6_all) ? '0' : round($d6_all / $count, 2) ?? '0';
            $result[$count]['d7']              = empty($d7_all) ? '0' : round($d7_all / $count, 2) ?? '0';
            $result[$count]['d14']             = empty($d14_all) ? '0' : round($d14_all / $count, 2) ?? '0';
            $result[$count]['d30']             = empty($d30_all) ? '0' : round($d30_all / $count, 2) ?? '0';
            $result[$count]['d45']             = empty($d45_all) ? '0' : round($d45_all / $count, 2) ?? '0';
            $result[$count]['d60']             = empty($d60_all) ? '0' : round($d60_all / $count, 2) ?? '0';
            $result[$count]['d90']             = empty($d90_all) ? '0' : round($d90_all / $count, 2) ?? '0';
            $result[$count]['d120']            = empty($d120_all) ? '0' : round($d120_all / $count, 2) ?? '0';
            $result[$count]['d180']            = empty($d180_all) ? '0' : round($d180_all / $count, 2) ?? '0';
            $result[$count]['d360']            = empty($d360_all) ? '0' : round($d360_all / $count, 2) ?? '0';
        }
        //返回结果
        return [
            "msg"   => '操作成功',
            "code"  => 0,
            "data"  => $result,
            "count" => $count,
        ];
    }

}
